package com.lizonghui.googleplay.fragment;

import android.graphics.Color;
import android.os.SystemClock;
import android.view.View;
import android.widget.TextView;

import com.lizonghui.googleplay.base.BaseFragment;
import com.lizonghui.googleplay.base.LoadingPager;
import com.lizonghui.googleplay.protocol.RecommendProtocol;
import com.lizonghui.googleplay.utils.UIUtils;
import com.lizonghui.googleplay.views.flyin_out.ShakeListener;
import com.lizonghui.googleplay.views.flyin_out.StellarMap;

import java.io.IOException;
import java.util.List;
import java.util.Random;

/**
 * Created by Administrator on 2016/6/16.
 */
public class RecommendFragment extends BaseFragment {

    private List<String> mList;
    private RecommendAdapter mAdapter;
    private ShakeListener mShakeListener;

    //初始化成功后视图
    @Override
    public View initSuccessView() {
        //自定义的一个控件,得到view
        final StellarMap map = new StellarMap(UIUtils.getContext());

        //data,去绑定数据
        mAdapter = new RecommendAdapter();
        map.setAdapter(mAdapter);

        //第一页没有展示解决
        map.setGroup(0, true);
        //拆分屏幕
        map.setRegularity(15,20);


        //摇一摇切换
        mShakeListener = new ShakeListener(UIUtils.getContext());
        mShakeListener.setOnShakeListener(new ShakeListener.OnShakeListener() {
            @Override
            public void onShake() {
                //摇一摇切到下一页
                //得到当前页
                int currentGroup = map.getCurrentGroup();
                if (currentGroup == mAdapter.getGroupCount() - 1) {
                    //最后一组的话
                    currentGroup = 0;
                } else {
                    currentGroup++;
                }
                //设置展示currentGroup
                map.setGroup(currentGroup, true);
            }
        });
        return map;
    }

    //摇一摇和生命周期绑定,resume.在当前视野中
    @Override
    public void onResume() {
        if(mShakeListener != null){
            mShakeListener.resume();
        }
        super.onResume();
    }
    //摇一摇和生命周期绑定,pause.刚离开视野
    @Override
    public void onPause() {
        if(mShakeListener != null){
            mShakeListener.pause();
        }
        super.onPause();
    }

    class RecommendAdapter implements StellarMap.Adapter {
        //数据集mList
        public static final int PAGESIZE = 15;

        @Override
        public int getGroupCount() {//一个有多少组
            if (mList.size() % PAGESIZE == 0) {
                return mList.size() / PAGESIZE;//刚好整除
            } else {
                return mList.size() / PAGESIZE + 1;//有余数，要多一组
            }
        }

        @Override
        public int getCount(int group) {//每组多少个数据
            if (mList.size() % PAGESIZE == 0) {//刚好整除
                return PAGESIZE;
            } else {//如果不整除
                if (group == getGroupCount() - 1) {//如果是最后一组
                    return mList.size() % PAGESIZE;
                } else {
                    return PAGESIZE;//不是最后一组
                }
            }
        }

        @Override
        public View getView(int group, int position, View convertView) {
            //view
            TextView tv = new TextView(UIUtils.getContext());
            //data
            int index = group * PAGESIZE + position;//得到选中的是集合第几个数据
            String data = mList.get(index);//从集合中拿数据

            //随机大小
            Random random = new Random();
            tv.setTextSize(random.nextInt(4) + 12);//随机获得12-16

            //随机颜色,透明值，红，绿，蓝
            int alpah = 220;//完全不透明
            int red = random.nextInt(190) + 30;//30 -220
            int green = random.nextInt(190) + 30;
            int blue = random.nextInt(190) + 30;
            int argb = Color.argb(alpah,red,green,blue);
            tv.setTextColor(argb);

            tv.setText(data);
            return tv;
        }

        @Override
        public int getNextGroupOnPan(int group, float degree) {
            return 0;
        }

        @Override
        public int getNextGroupOnZoom(int group, boolean isZoomIn) {
            return 0;
        }
    }

    @Override
    public LoadingPager.LoadedResultState initData() {
        SystemClock.sleep(2000);// 模拟网络耗时2s种
        //要访问网络，就要获取协议请求对象
        RecommendProtocol recommendProtocol = new RecommendProtocol();
        //用协议请求去加载数据
        try {
            mList = recommendProtocol.loadData(0);//请求得到数据
            //校验数据
            return checkResData(mList);
        } catch (IOException e) {
            e.printStackTrace();
            return LoadingPager.LoadedResultState.ERROR;
        }
    }
}
